package com.qiantu.api.entity;

import com.qiantu.api.db.greendao.DaoSession;
import com.qiantu.api.db.greendao.DeviceBeanDao;
import com.qiantu.api.db.greendao.RoomBeanDao;
import com.qiantu.api.db.greendao.SceneBeanDao;

import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.JoinProperty;
import org.greenrobot.greendao.annotation.Keep;
import org.greenrobot.greendao.annotation.ToMany;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Unique;

import java.util.List;
import com.qiantu.api.db.greendao.DaoSession;
import com.qiantu.api.db.greendao.SceneBeanDao;
import com.qiantu.api.db.greendao.DeviceBeanDao;
import com.qiantu.api.db.greendao.RoomBeanDao;

@Entity
public class RoomBean {

    public static final long COMMON_ROOM_ID = -10000;//通用房间
    public static final String ROOM_COMMON_SERIALNO = "00000000-0000-0000-0000-000000000000";
    @Id(autoincrement = true)
    private Long id;
    @Unique
    private String roomSerialNo;
    private String roomName;
    private String roomTypeId;
    private String floorSerialNo;
    private String floorName;
    private String houseSerialNo;
    private String houseName;
    private boolean isDeafultFloor;
    private String createTime;
    private int sort;
    /**
     * 默认 : 0
     * 与房间绑定 : 1
     * 通用 : 2
     * 与用户绑定 : 3
     */
    private int linkType;
    private  String picUrl;
    private int shareCount;
    @ToMany(joinProperties = {
            @JoinProperty(name = "roomSerialNo", referencedName = "roomSerialNo")
    })
    private List<DeviceBean> devices;
    @ToMany(joinProperties = {
            @JoinProperty(name = "roomSerialNo", referencedName = "roomSerialNo")
    })
    private List<SceneBean> scenes;

    @Transient
    private int deviceSize = -1;
    @Transient
    private int sceneSize = -1;
    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;
    /** Used for active entity operations. */
    @Generated(hash = 1345831925)
    private transient RoomBeanDao myDao;


    @Generated(hash = 2135387174)
    public RoomBean() {
    }

    @Generated(hash = 613680344)
    public RoomBean(Long id, String roomSerialNo, String roomName, String roomTypeId,
            String floorSerialNo, String floorName, String houseSerialNo, String houseName,
            boolean isDeafultFloor, String createTime, int sort, int linkType, String picUrl,
            int shareCount) {
        this.id = id;
        this.roomSerialNo = roomSerialNo;
        this.roomName = roomName;
        this.roomTypeId = roomTypeId;
        this.floorSerialNo = floorSerialNo;
        this.floorName = floorName;
        this.houseSerialNo = houseSerialNo;
        this.houseName = houseName;
        this.isDeafultFloor = isDeafultFloor;
        this.createTime = createTime;
        this.sort = sort;
        this.linkType = linkType;
        this.picUrl = picUrl;
        this.shareCount = shareCount;
    }

    public String getRoomSerialNo() {
        return roomSerialNo;
    }

    public void setRoomSerialNo(String roomSerialNo) {
        this.roomSerialNo = roomSerialNo;
    }

    public String getHouseSerialNo() {
        return houseSerialNo;
    }

    public void setHouseSerialNo(String houseSerialNo) {
        this.houseSerialNo = houseSerialNo;
    }

    public String getFloorSerialNo() {
        return floorSerialNo;
    }

    public void setFloorSerialNo(String floorSerialNo) {
        this.floorSerialNo = floorSerialNo;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public String getFloorName() {
        return floorName;
    }

    public void setFloorName(String floorName) {
        this.floorName = floorName;
    }

    public String getHouseName() {
        return houseName;
    }

    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }

    public boolean isDeafultFloor() {
        return isDeafultFloor;
    }

    public void setDeafultFloor(boolean deafultFloor) {
        isDeafultFloor = deafultFloor;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public int getLinkType() {
        return linkType;
    }

    public void setLinkType(int linkType) {
        this.linkType = linkType;
    }

    public String getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }

    public void setDevices(List<DeviceBean> devices) {
        this.devices = devices;
    }

    public void setScenes(List<SceneBean> scenes) {
        this.scenes = scenes;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getDeviceSize() {
        return deviceSize;
    }

    public void setDeviceSize(int deviceSize) {
        this.deviceSize = deviceSize;
    }

    public int getSceneSize() {
        return sceneSize;
    }

    public void setSceneSize(int sceneSize) {
        this.sceneSize = sceneSize;
    }

    @Keep
    public List<DeviceBean> getDevices() {
        if (devices == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            DeviceBeanDao targetDao = daoSession.getDeviceBeanDao();
            List<DeviceBean> devicesNew = targetDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo),DeviceBeanDao.Properties.LinkType.eq(1))
                    .orderAsc(DeviceBeanDao.Properties.Sort)
                    .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                    .list();
            synchronized (this) {
                if (devices == null) {
                    devices = devicesNew;
                }
            }
        }
        return devices;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 1428662284)
    public synchronized void resetDevices() {
        devices = null;
    }

    @Keep
    public List<SceneBean> getScenes() {
        if (scenes == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            SceneBeanDao targetDao = daoSession.getSceneBeanDao();
            List<SceneBean> sencesNew = targetDao.queryBuilder()
                    .where(SceneBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                    .where(SceneBeanDao.Properties.SceneType.eq(1))
                    .orderAsc(SceneBeanDao.Properties.Sort)
                    .orderDesc(SceneBeanDao.Properties.CreateTime)
                    .list();
            synchronized (this) {
                if (scenes == null) {
                    scenes = sencesNew;
                }
            }
        }
        return scenes;
    }
    
    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    public boolean getIsDeafultFloor() {
        return this.isDeafultFloor;
    }

    public void setIsDeafultFloor(boolean isDeafultFloor) {
        this.isDeafultFloor = isDeafultFloor;
    }

    public int getSort() {
        return this.sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 187241317)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getRoomBeanDao() : null;
    }

    public String getRoomTypeId() {
        return this.roomTypeId;
    }

    public void setRoomTypeId(String roomTypeId) {
        this.roomTypeId = roomTypeId;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 1916439147)
    public synchronized void resetScenes() {
        scenes = null;
    }

    public int getShareCount() {
        return this.shareCount;
    }

    public void setShareCount(int shareCount) {
        this.shareCount = shareCount;
    }
}
